package com.richman.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

import com.richman.controller.AutoBetCentralManager;
import com.richman.controller.DecreaseRatioClient;
import com.richman.ps38.entity.BetAdyData;
import com.richman.ps38.entity.Event;
import com.richman.utils.Common;
import com.richman.view.BetLogPane.MyTableModel;

public class BetLimitPane extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8L;
	
	private  Vector<String[]> detailsData = new Vector<String[]>();
	
	public Vector<Event> gameDetailsVec = new Vector<Event>(); 
	
	
	
	
	//BrowserPanel bp = new BrowserPanel();
	
	
	
	
	
	MyTableModel tableMode = new MyTableModel();

	
	JTable table = null;

	/**
	 * Create the panel.
	 */
	public BetLimitPane() {
		setLayout(new BorderLayout(0, 0));

		JPanel funBtnPane = new JPanel();
		
		JButton cleanButton = new JButton("清空数据");
		cleanButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				int n = JOptionPane.showConfirmDialog(null, "确认要清空数据吗?", "清空数据",
						JOptionPane.YES_NO_OPTION);
				if (n == JOptionPane.YES_OPTION && DecreaseRatioClient.betCtrMng != null) {					
					DecreaseRatioClient.betCtrMng.clearBetLogData();
				}
				
			}
		});
		
		cleanButton.setBounds(20, 10, 60, 20);
		
		funBtnPane.add(cleanButton);
		
		add(funBtnPane, BorderLayout.NORTH);
		
		
	    table = new JTable(tableMode);
	    
	    table.getColumnModel().getColumn(0).setPreferredWidth(30);
	    
	    table.getColumnModel().getColumn(2).setPreferredWidth(120);
	    table.getColumnModel().getColumn(3).setPreferredWidth(130);
	    table.getColumnModel().getColumn(4).setPreferredWidth(200);
	    table.getColumnModel().getColumn(5).setPreferredWidth(100);
	    
	    
	    table.getColumnModel().getColumn(8).setPreferredWidth(180);
	    table.getColumnModel().getColumn(11).setPreferredWidth(180);
	    
	    table.getColumnModel().getColumn(14).setPreferredWidth(30);
	    
	    table.setRowHeight(30);
	    
	    table.setFont(new java.awt.Font("黑体", Font.PLAIN, 15));
		
		

//		
//		TableColumn hgres = table.getColumn("皇冠结果");   
//        TableColumn ps38res = table.getColumn("PS38结果");   
//		
//		
//		
//		
//		
//		
//        DefaultTableCellRenderer resRender = new DefaultTableCellRenderer() {   
//
//            public void setValue(Object value) { //重写setValue方法，从而可以动态设置列单元字体颜色   
//
//               
//            	String str = value.toString();
//            	
//				
//				
//				if(str.contains("成功")){
//					
//					setBackground(Color.GREEN);
//					
//					
//					
//				}else if(str.contains("失败")){
//	
//					setBackground(Color.red);
//				}else {
//					setBackground(Color.yellow);
//				}
//				
//				
//				setForeground(Color.BLACK);
//					
//
//
//            }   
//
//        };   
//		
//        
//        hgres.setCellRenderer(resRender);
//        ps38res.setCellRenderer(resRender);
		
        
		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane, BorderLayout.CENTER);
		
		
		
		//add(bp, BorderLayout.SOUTH);
		
		
		
		
		//双击table事件
/*	    table.addMouseListener(new MouseAdapter(){

            public void mouseClicked(MouseEvent e) {
            	
            	
            	
          	  if(e.getClickCount() == 2){
          		  
          		  if(YinNiPan.blogintohg == false){
          			JOptionPane.showMessageDialog(null,"皇冠账号未登录，无法投注");
          			return;
          		  }
          		  
          		  
          		  
              	  int row =((JTable)e.getSource()).rowAtPoint(e.getPoint()); //获得行位置 
              	  
              	  if(detailsData.elementAt(row)[0].equals("")){
              		  return;
              	  }else{
              		  
              		String teamh = detailsData.elementAt(row)[3].split("-vs-")[0];
              		String time = detailsData.elementAt(row)[2];
              		  
              		  for(int i = 0; i < gameDetailsVec.size(); i++){
              			  
              			  
              			  
              			  if(teamh.equals(gameDetailsVec.elementAt(i).teamh) && time.equals(gameDetailsVec.elementAt(i).datetime)){
              				  betwnd.setgame(gameDetailsVec.elementAt(i));
              				  betwnd.setVisible(true);
              				  break;
              			  }
              		  }
              	  }
              	  
          	  }else{
          		  return;
          	  }

            }
      });*/
		

	}
	
	
	
	public void updateBetLogTable(Vector<BetAdyData> gamedetails){
		
		try{
			
			if(detailsData.size() != 0){
				detailsData.clear();
			}
			
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			int betcount = gamedetails.size();
			
			for(int i = 0; i < gamedetails.size(); i++) {
				
				BetAdyData game = gamedetails.elementAt(i);
				if(!game.hgbetres.equals("-4")) {
					continue;
				}
				
				
				
				String hgbetres = "";
				
				if(game.hgbetres.equals("1")) {
					hgbetres = "成功";
				}else if(game.hgbetres.equals("2")) {
					hgbetres = "确认成功";
				}else if(game.hgbetres.equals("0")) {
					hgbetres = "注单待确认";
				}else if(game.hgbetres.equals("-1")) {
					hgbetres = "超过信用额度";
				}else if(game.hgbetres.equals("-2")) {
					hgbetres = "失败 ";
				}else if(game.hgbetres.equals("-3")) {
					hgbetres = "注单取消，投注失败";
				}else if(game.hgbetres.equals("-4")) {
					hgbetres = "股东限额，投注失败";
					if(!AutoBetCentralManager.limistGames.contains(game.hgeventid)) {
						AutoBetCentralManager.limistGames.add(game.hgeventid);
					}
				}else {
					hgbetres = game.hgbetres;
				}
				
				
				String ps38betres = "";
				
				if(game.ps38betres.equals("")) {
					ps38betres = "失败";
				}else if(game.ps38betres.equals("2")) {
					ps38betres = "确认成功";
				}else if(game.ps38betres.equals("1")) {
					ps38betres = "成功";
				}else if(game.ps38betres.equals("0")) {
					ps38betres = "注单待确认";
				}else if(game.ps38betres.equals("-3")) {
					ps38betres = "注单取消，投注失败";
				}else if(game.ps38betres.equals("-4")) {
					ps38betres = "注单暂时无法确认,请人工确认";
				}else {
					ps38betres = game.ps38betres;
				}
				
				
				if(game.ps38rebetflag != null && game.ps38rebetflag.equals("1")) {
					ps38betres = "补单-" + ps38betres;
				}

				
				
				//{ "序号", "联赛", "时间", "球队", "投注类型", "投注盘口","合并赔率", "皇冠注单", "皇冠金额", "皇冠投注时间","皇冠结果", "PS38注单", "PS38金额 ", "PS38投注时间 ", "PS38结果"};
				String[] item = {Integer.toString(betcount - i), game.currenttime, game.league, game.time, game.teams, game.bettype, game.betpankou, game.mergeratio, game.hgbettype,
								game.hgbetgold,  hgbetres, game.ps38bettype, game.ps38betgold,  ps38betres,game.score,game.websitetype,""};
				
				detailsData.add(0,item);
				
				
				
			}
			
			

			for(int i = 0; i < detailsData.size(); i++ ) {
				int limitTimes = 0;
				int limitGold = 0;
				if(detailsData.elementAt(i)[10].contains("股东限额")) {
					for(int j = 0; j <detailsData.size(); j++) {
						if(detailsData.elementAt(i)[4].equals(detailsData.elementAt(j)[4]) && detailsData.elementAt(j)[10].contains("股东限额")) {
							limitTimes++;
							limitGold += Integer.parseInt(detailsData.elementAt(j)[9]);
						}
					}
					
					for(int j = 0; j <detailsData.size(); j++) {
						if( detailsData.elementAt(i)[4].equals(detailsData.elementAt(j)[4]) && detailsData.elementAt(j)[10].contains("股东限额") ) {
							detailsData.elementAt(i)[16] = Integer.toString(limitTimes) + "/" + Integer.toString(limitGold);
						}
					}
					
				}
			}
			
			
			
			
			setOneRowBackgroundColor();
			
			tableMode.updateTable();

			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	
	
    public void setOneRowBackgroundColor() {
    	

    	
        try {  
        	
            DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
            	
            	
  
                public Component getTableCellRendererComponent(JTable table,  
                        Object value, boolean isSelected, boolean hasFocus,  
                        int row, int column) {  

                	
                    if (row%2==1) {  
                    	setBackground(new Color(246,246,246));  
                        
                    }else{  
                        setBackground(new Color(222,222,243));  
                    	//setBackground(new Color(1,1,1));  
                      
                    }  
                    
                    setHorizontalAlignment(JLabel.CENTER);
  
                    return super.getTableCellRendererComponent(table, value,  
                            isSelected, hasFocus, row, column);  
                }  
            };  
            
            
            DefaultTableCellRenderer resRender = new DefaultTableCellRenderer() {   

                public void setValue(Object value) { //重写setValue方法，从而可以动态设置列单元字体颜色   

                   
                	String str = value.toString();
                	
    				
    				
    				if(str.contains("成功")){
    					
    					setBackground(Color.GREEN);

    				}else if(str.contains("失败")){    	
    					setBackground(Color.red);
    				}else {
    					setBackground(Color.yellow);
    				}
    				
    				
    				setForeground(Color.BLACK);
    					
    				setText((value == null) ? "" : str);
    				
    				 setHorizontalAlignment(JLabel.CENTER);

                }   

            };  
            
            
            
            
            DefaultTableCellRenderer ratioRender = new DefaultTableCellRenderer() {   

                public void setValue(Object value) { //重写setValue方法，从而可以动态设置列单元字体颜色   

                   
                	String str = value.toString();
                	
                	
                	setForeground(Color.BLACK);
                	
    				if(Common.isNum(str)) {
    					double ratio = Double.parseDouble(str);
    					if(ratio - DecreaseRatioClient.mainWnd.getbetodd() < 0.0) {
    						setForeground(Color.red);
    	    				
    					}
    				}
    				
    				//setBackground(new Color(246,246,246)); 
    				
    				
    					
    				setText((value == null) ? "" : str);
    				
    				 setHorizontalAlignment(JLabel.CENTER);

                } 
                
                
                public Component getTableCellRendererComponent(JTable table,  
                        Object value, boolean isSelected, boolean hasFocus,  
                        int row, int column) {  

                	
                    if (row%2==1) {  
                    	setBackground(new Color(246,246,246));  
                        
                    }else{  
                        setBackground(new Color(222,222,243));  
                    	//setBackground(new Color(1,1,1));  
                      
                    }  
                    
                    setHorizontalAlignment(JLabel.CENTER);
  
                    return super.getTableCellRendererComponent(table, value,  
                            isSelected, hasFocus, row, column);  
                }  
                
                
                

            }; 
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            int columnCount = table.getColumnCount();  
            for (int i = 0; i < columnCount; i++) {  
            	
            	String columnname = table.getColumnName(i);
            	
            	if(columnname.equals("皇冠结果") || columnname.equals("结果")) {
            		table.getColumn(columnname).setCellRenderer(resRender);
            	//	continue;
            	}else if(columnname.equals("合并赔率")){
            		table.getColumn(columnname).setCellRenderer(ratioRender);
            		
            	}else {
            		table.getColumn(columnname).setCellRenderer(tcr);
            	}
            	
            	
            }  
        } catch (Exception ex) {  
            ex.printStackTrace();  
        }  
    
    }  
	
	
	
	

    public class MyTableModel extends AbstractTableModel  
    {  
        /**
		 * 
		 */
		private static final long serialVersionUID = 10086L;
		/* 
         * 这里和刚才一样，定义列名和每个数据的值 
         */  
        String[] columnNames =          
        	{ "序号", "时间","联赛", "比赛时间", "球队", "投注类型", "投注盘口", "合并赔率",  "皇冠注单", "皇冠金额", "皇冠结果", "副网注单", "金额 ",  "结果", "投注比分","副网", "限额手数/金额"};

  
        /** 
         * 构造方法，初始化二维数组data对应的数据 
         */  
        public MyTableModel()  
        {  

        }  
  
        // 以下为继承自AbstractTableModle的方法，可以自定义  
        /** 
         * 得到列名 
         */  
        @Override  
        public String getColumnName(int column)  
        {  
            return columnNames[column];  
        }  
          
        /** 
         * 重写方法，得到表格列数 
         */  
        @Override  
        public int getColumnCount()  
        {  
            return columnNames.length;  
        }  
  
        /** 
         * 得到表格行数 
         */  
        @Override  
        public int getRowCount()  
        {         
	        if(null == detailsData){
	    		return 0;
	    	}
            return detailsData.size();  
        }  
  
        /** 
         * 得到数据所对应对象 
         */  
        @Override  
        public Object getValueAt(int rowIndex, int columnIndex)  
        {  
            //return data[rowIndex][columnIndex];
        	return detailsData.elementAt(rowIndex)[columnIndex];
        }  
  
        /** 
         * 得到指定列的数据类型 
         */  
        @Override  
        public Class<?> getColumnClass(int columnIndex)  
        {  
        	
        	return String.class;
        	
//        	if(detailsData.size() == 0){
//        		return String.class;
//        	}
//
//            return detailsData.elementAt(0)[columnIndex].getClass();
        	
        }  
  
        /** 
         * 指定设置数据单元是否可编辑.这里设置"姓名","学号"不可编辑 
         */  
        @Override  
        public boolean isCellEditable(int rowIndex, int columnIndex)  
        {  
            return false;
        }  
          
        /** 
         * 如果数据单元为可编辑，则将编辑后的值替换原来的值 
         */  
        @Override  
        public void setValueAt(Object aValue, int rowIndex, int columnIndex)  
        {  
            detailsData.elementAt(rowIndex)[columnIndex] = (String)aValue;  
            /*通知监听器数据单元数据已经改变*/  
            fireTableCellUpdated(rowIndex, columnIndex);  
        }  
        
        public void updateTable(){
        	fireTableDataChanged();
        }
        
  
    }  
	
	
	
	
	
	
	
	
	
}
